package com.egame.manage.biz.web.controller.pu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.common.servlet.WebUtils;
import cn.egame.common.util.Utils;
import cn.egame.ext.op.AdvDetail;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.EnumType.GameTagType;
import cn.egame.interfaces.EnumType.NetworkType;
import cn.egame.interfaces.fl.FileInfo;
import cn.egame.interfaces.fl.FileUsedType;
import cn.egame.interfaces.fl.FileUtils;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.gc.GameTagLinkInfo;
import cn.egame.interfaces.pu.AppParameter;

import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.ManageClientExt;
import com.egame.manage.biz.server.gc.GameServiceImpl;
import com.egame.manage.biz.server.gc.GameTagServiceImpl;
import com.egame.manage.biz.server.op.StoreAdvertiseServiceImpl;
import com.egame.manage.biz.server.pu.StoreSortServiceImpl;
import com.egame.manage.biz.utils.ConstVar;
import com.egame.manage.biz.utils.ConstantVar;
import com.egame.manage.biz.web.controller.BaseController;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/store/quick")
public class QuickNavigateController extends BaseController {

	public QuickNavigateController(){		
	}
	public QuickNavigateController(HttpServletRequest req,
			HttpServletResponse resp) {
		super(req, resp);
	}
	
	@RequestMapping(value = "/advertise/list", method = {RequestMethod.GET,RequestMethod.POST})	
	public ModelAndView listEditPosition(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ModelAndView mv = new ModelAndView("/new/op/store/quickNavigateList");
		AdvDetail bean = new AdvDetail();
		String banner = WebUtils.getString(req, "banner","");
		String beginTime = WebUtils.getString(req, "beginTime","");
		String endTime = WebUtils.getString(req, "endTime","");
		bean.setTitle(banner);
		bean.setBeginString(beginTime);
		bean.setEndString(endTime);
		if(""!=beginTime){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date beginDate = sdf.parse(beginTime);
			bean.setBeginTime(beginDate);
		}
		if(""!=endTime){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date endDate = sdf.parse(endTime);
			bean.setEndTime(endDate);
		}
		bean.setStatus(1);//开启状态
		bean.setEfsId(0);
		List<AdvDetail> advHomeList = null;
			//首页单屏广告位
		bean.setPositionTypeId(ConstantVar.STORE_SAMLL_EDIT_TYPE);
		advHomeList = ManageClientExt.getInstance().listAdvDetailByBean(appId,loginUserId,bean); 
		mv.addObject("advHomeList1", advHomeList);
		mv.addObject("banner", banner);
		mv.addObject("beginTime", beginTime);
		mv.addObject("endTime", endTime);
		return mv;			
	}
	
	//新增跳转
		@RequestMapping(value = "/advertise/set/show", method = {RequestMethod.GET,RequestMethod.POST})
		public ModelAndView showNewAdvertisement(HttpServletRequest req, HttpServletResponse resp)throws Exception{
			return new ModelAndView("/new/op/store/addQuickSort");
		}	
		//新增提交
		@RequestMapping(value = "/advertise/new", method = {RequestMethod.GET,RequestMethod.POST})	
		public ModelAndView addAppSort(HttpServletRequest req, HttpServletResponse resp,AdvDetail bean,String beginAdv,String endAdv) throws Exception{
			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			try{							
				bean.setBeginTime(Utils.parseDate(bean.getBeginString()));
				bean.setEndTime(Utils.parseDate(bean.getEndString()));					
			}catch(Exception e){
				bean.setBeginTime(new Date());
				bean.setEndTime(new Date());
			}	
			bean.setOperationId((int) loginUserId);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date beginTime = sdf.parse(beginAdv);
			Date endTime = sdf.parse(endAdv);
			bean.setBeginTime(beginTime);
			bean.setEndTime(endTime);
			bean.setStatus(1);
			bean.setEfsId(0);
			bean.setPositionTypeId(ConstantVar.STORE_SAMLL_EDIT_TYPE);
			int maxSortNo = StoreAdvertiseServiceImpl.getInstance().getMaxSortNo(bean.getRecordType(), bean.getPositionTypeId());
			bean.setSortNo(1 + maxSortNo);	
			bean.setInsertTime(new Date());
			bean.setRecordType(1);//广告
			ManageClientExt.getInstance().addAdvDetail(appId, loginUserId, bean);
			return listEditPosition(req,resp);
		}
		//删除
		@RequestMapping(value = "/advertise/delete", method = {RequestMethod.GET,RequestMethod.POST})	
		public ModelAndView deleteAppSort(HttpServletRequest req, HttpServletResponse resp) throws Exception{
			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			int advertiseId = WebUtils.getInt(req,"id",0);	
			//可用做提示框验证条件
			int deleteId = StoreAdvertiseServiceImpl.getInstance().deleteAdvertisement(appId, loginUserId, advertiseId);
			return listEditPosition(req,resp);
		}
		//编辑跳转
		@RequestMapping(value = "/advertise/edit/show", method = {RequestMethod.GET,RequestMethod.POST})
		public ModelAndView editAppSort(HttpServletRequest req, HttpServletResponse resp)throws Exception{
			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			int advertiseId = WebUtils.getInt(req,"id",0);	
			AdvDetail bean = ManageClientExt.getInstance().getAdvDetailById(appId, loginUserId, advertiseId);		
			ModelAndView mv = new ModelAndView("/new/op/store/editQuickSort"); 
			mv.addObject("bean", bean);
			return mv;
		}
		
		//编辑提交
		@RequestMapping(value = "/advertise/set/edit", method = {RequestMethod.GET,RequestMethod.POST})
		public ModelAndView setHXAdvertisement(AdvDetail bean,HttpServletRequest req, HttpServletResponse resp)throws Exception{

			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			try{							
				bean.setBeginTime(Utils.parseDate(bean.getBeginString()));
				bean.setEndTime(Utils.parseDate(bean.getEndString()));					
			}catch(Exception e){
				bean.setBeginTime(new Date());
				bean.setEndTime(new Date());
			}	
			
			int maxSortNo = StoreAdvertiseServiceImpl.getInstance().getMaxSortNo(bean.getRecordType(), bean.getPositionTypeId());
			bean.setSortNo(1 + maxSortNo);	
			bean.setInsertTime(new Date());		
			ManageClientExt.getInstance().updateAdvDetail(appId, loginUserId, bean);
			
			return listEditPosition(req,resp);
		}
		
		//历史数据
		@RequestMapping(value = "/advertise/history/list", method = {RequestMethod.GET,RequestMethod.POST})
		public ModelAndView listHXHistoryActivity(HttpServletRequest req, HttpServletResponse resp) throws Exception{
			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			ModelAndView mv = new ModelAndView("/new/op/store/quickHisAdvertise");
			AdvDetail bean = new AdvDetail();
			
			bean.setStatus(0);//关闭状态
			bean.setPositionTypeId(ConstantVar.STORE_SAMLL_EDIT_TYPE);
			List<AdvDetail> advHisList = ManageClientExt.getInstance().listAdvDetailByBean(appId,loginUserId,bean); 
			mv.addObject("advHisList", advHisList);
			
			return mv;
		}
		//上移
		@RequestMapping(value = "/advertise/upper", method = {RequestMethod.GET,RequestMethod.POST})
		public ModelAndView upperSortNo(HttpServletRequest req, HttpServletResponse resp)throws Exception{
			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			int advertiseId = WebUtils.getInt(req,"id",0);
			StoreAdvertiseServiceImpl.getInstance().upper(appId, loginUserId, advertiseId);		
			return listEditPosition(req,resp);
		}	
		//下移
		@RequestMapping(value = "/advertise/lower", method = {RequestMethod.GET,RequestMethod.POST})
		public ModelAndView lowerSortNo(HttpServletRequest req, HttpServletResponse resp)throws Exception{
			if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
				UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
				loginUserId = userBean.getOperatorId();
			}
			int advertiseId = WebUtils.getInt(req,"id",0);
			StoreAdvertiseServiceImpl.getInstance().lower(appId, loginUserId, advertiseId);
			return listEditPosition(req,resp);
		}	
}
